package com.sample.model;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.validator.Digits;
import org.hibernate.validator.Email;
import org.hibernate.validator.NotEmpty;

@Entity
@Table(name="user")
public class User {
	@Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id", length=11, nullable=false)
	private Long id;
	
	@Column(length=32)
	private String login;
	
	@NotEmpty
	@Column(name="first_name", length=50, nullable=false)
	private String firstName;
	
	@NotEmpty
	@Column(name="last_name", length=50, nullable=false)
	private String lastName;
	
	@Email
	@Column(length=50)
	private String email;
	
	@Column(name="job_title", length=50)
	private String jobTitle;
	
	@Digits(integerDigits=7, fractionalDigits=3)
	@Column(name="bill_rate", scale=10, precision=3)
	private Double billRate;
	
	private String password;
	
	@ManyToMany(targetEntity=Role.class)
	@JoinTable(name="user_x_role", 
			joinColumns={@JoinColumn(name="user_id")},
			inverseJoinColumns= {@JoinColumn(name="role_id")})
	private List<Role> roles;
	
	/**
	 * This is marked cascade {} so hibernate will not automatically update the database from it.
	 * Instead we're doing the update manually in the DAO.
	 */
	@OneToMany(mappedBy="user", cascade = {})
	private List<UserProject> projects;

	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getJobTitle() {
		return jobTitle;
	}
	public void setJobTitle(String jobTitle) {
		this.jobTitle = jobTitle;
	}
	public Double getBillRate() {
		return billRate;
	}
	public void setBillRate(Double billRate) {
		this.billRate = billRate;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public List<Role> getRoles() {
		return roles;
	}
	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}
	public List<UserProject> getProjects() {
		return projects;
	}
	public void setProjects(List<UserProject> projects) {
		this.projects = projects;
	}
}
